package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IDepartmentService;
import cn.wolfcode.rbac.util.JsonResult;
import cn.wolfcode.rbac.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService service;

    //部门列表
    @RequiredPermission(name = "部门页面",expression = "department:list")
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model){
        PageInfo pageInfo = this.service.query(qo);
        model.addAttribute("result",pageInfo);
        return "department/list";
    }
    //部门在修改之前需要做的查询工作
    /*@RequiredPermission(name = "部门编辑",expression = "department:input")
    @RequestMapping("/input")
    public String input(Long id,Model model){
        if(id!=null){
            model.addAttribute("entity",this.service.get(id));
        }
        return "department/input";
    }*/

    //部门新增/编辑，调用同一个方法
    @RequiredPermission(name = "部门新增/编辑",expression = "department:saveOrUpdate")
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department entity){
        if(entity.getId()==null){
            this.service.saveOrUpdate(entity);
        }else {
            this.service.saveOrUpdate(entity);
        }
        return new JsonResult("操作成功",true);

    }
    //部门删除
    @RequiredPermission(name = "部门删除",expression = "department:delete")
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id){
        this.service.delete(id);
        return new JsonResult("操作成功",true);
    }
}
